package com.atkxp.web; /**
 * @declaration
 * @author user
 * @create 2022/9/24-17:41
 */

import com.atkxp.domain.User;
import com.atkxp.service.UserService;
import com.atkxp.service.impl.UserServiceImpl;
import com.atkxp.utils.WebUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

@WebServlet(name = "UserServlet", value = "/userServlet")
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();
    protected void isExistUsername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String username = req.getParameter("username");
        boolean existsUsername = userService.existUsername(username);
        Map<String, Boolean> map = new HashMap<>();
        map.put("existsUsername", existsUsername);
        Gson gson = new Gson();
        String toJson = gson.toJson(map);
        resp.getWriter().write(toJson);
    }
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User login = userService.login(new User(username, password, null));
        if(login != null){
            System.out.println("登录成功");
            HttpSession session = req.getSession();
            session.setAttribute("user", login);
            Cookie cookie = new Cookie("username", username);
            resp.addCookie(cookie);
            req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req,resp);
        }else{
            req.setAttribute("msg", "用户名或密码错误！");
            req.setAttribute("username", username);
            System.out.println("登录失败");
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
        }
    }
    protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String token = (String)request.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        request.getSession().removeAttribute(KAPTCHA_SESSION_KEY);

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String code = request.getParameter("code");
        User user = WebUtils.copyParameterToBean(request.getParameterMap(), new User());
        if (token != null && token.equalsIgnoreCase(code)) {
            if (userService.existUsername(username)) {
                System.out.println("用户名已存在！");
                request.setAttribute("msg", "用户名已存在！");
                request.setAttribute("username", username);
                request.setAttribute("email", email);
                request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
            } else {
                userService.registUser(new User(username, password, email));
                request.getRequestDispatcher("/pages/user/regist_success.jsp").forward(request, response);
            }
        } else {
            request.setAttribute("msg", "验证码错误!");
            request.setAttribute("username", username);
            request.setAttribute("email", email);
            System.out.println("验证码错误");
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
        }
    }

    protected void logout (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.getSession().invalidate();//销毁Session;
        response.sendRedirect(request.getContextPath());
    }
}
